package excelOperate;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;

public class ExcelReader {
	
	public static List<String> read(String fileName){
		InputStream is;
		List<String> lst = new ArrayList<String>();
		try {
			is = new FileInputStream(fileName);
			HSSFWorkbook hssfWb = new HSSFWorkbook(new POIFSFileSystem(is));
			HSSFSheet sheet =  hssfWb.getSheetAt(0);
			System.out.println(sheet.getSheetName());
			Iterator<Row> rowIt = sheet.iterator();
			while(rowIt.hasNext()){
				HSSFRow row = (HSSFRow) rowIt.next();
				if(isEmpty(row)){
					break;
				}
				HSSFCell cell = (HSSFCell) row.getCell(1);
				lst.add(cell.toString());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lst;
	}
	
	public static boolean isEmpty(HSSFRow row){
		if(row == null){
			return true;
		}
		HSSFCell cell = row.getCell(0);
		if(cell == null || "".equals(cell.toString().trim())){
			return true;
		}
		return false;
	}

}
